package com.game.autoTool.project.web.common.exception;


import com.fasterxml.jackson.databind.JsonMappingException;
import com.game.autoTool.project.web.domain.resp.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import sun.security.validator.ValidatorException;

import java.util.stream.Collectors;

/**
 * @author
 * @ClassName: GlobalExceptionHandler
 * @Description: 全局异常处理器
 */
@Slf4j
@RestControllerAdvice
public class BizGlobalExceptionHandler {

    /**
     * The constant DATA_FORMAT_ERROR.
     */
    public static final String DATA_FORMAT_ERROR = "json数据格式错误";

    /**
     * Custom exception result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result<Object> requestException(Exception e) {
        log.error("GlobalExceptionHandler.requestException error : ", e);
        return Result.failed("请求异常");
    }

    /**
     * Illegal arg result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseBody
    @ExceptionHandler(ValidatorException.class)
    public Result<Object> validatorException(ValidatorException e) {
        log.error("GlobalExceptionHandler.validatorException error : ", e);

        String message = e.getMessage().replace("VALIDATOR_BEGIN:", "")
                .replace(":VALIDATOR_END", "");
        return Result.failed("参数异常");
    }


    /**
     * Illegal arg result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseBody
    @ExceptionHandler(CustomException.class)
    public Result<Object> customException(CustomException e) {
        log.error("GlobalExceptionHandler.customException error : ", e);
        return Result.failed(e.getMessage());
    }

    /**
     * Illegal arg result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseBody
    @ExceptionHandler(IllegalArgumentException.class)
    public Result<Object> illegalArgumentException(IllegalArgumentException e) {
        log.error("GlobalExceptionHandler.illegalArgumentException error : ", e);
        return Result.failed("非法参数异常");
    }

    /**
     * Handle validation exceptions result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<Object> methodArgumentNotValidException(MethodArgumentNotValidException e) {
        log.error("GlobalExceptionHandler.methodArgumentNotValidException error : ", e);
        final String errorMsg = e.getBindingResult().getAllErrors().stream()
                .map(DefaultMessageSourceResolvable::getDefaultMessage)
                .collect(Collectors.joining(";"));
        return Result.failed("方法参数无效异常");
    }


    /**
     * Json mapping exception result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(JsonMappingException.class)
    @ResponseBody
    public Result<Object> jsonMappingException(JsonMappingException e) {
        log.error("GlobalExceptionHandler.jsonMappingException error : ", e);
        return Result.failed("json映射异常异常");
    }

    /**
     * Bind exception result.
     *
     * @param e the e
     * @return the result
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result<Object> bindException(BindException e) {
        log.error("GlobalExceptionHandler.bindException error : ", e);
        return Result.failed("绑定异常");
    }
}
